﻿package com.tomvian.maths.areas
{
	import com.tomvian.maths.Vector2D;
	
	/**
	 * LineArea
	 * @author Thomas Vian
	 */
	public class LineArea extends Area
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		private var _point1:Vector2D;
		private var _point2:Vector2D;
		private var _diff:Vector2D;
		
		private var _length:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function LineArea(point1:Vector2D, point2:Vector2D)
		{
			_point1 = point1;
			_point2 = point2;
			_diff = point2.subtract(point1);
			_length = _diff.length;
			area = _length;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function contains(vector:Vector2D):Boolean 
		{
			return uint((vector.x - _point1.x) * (_point2.y - _point1.y) * 10000) 
					== uint((_point2.x - _point1.x) * (vector.y - _point1.y) * 10000);
		}
		
		override public function random():Vector2D 
		{			
			var rand:Number = Math.random();
			
			return new Vector2D(_point1.x + _diff.x * rand, _point1.y + _diff.y * rand);
		}
	}
}